<template>
    <el-dialog :title="!dataForm.id ? '新增' : '修改'" :before-close="closeDailog" destroy-on-close
        :close-on-click-modal="false" :visible.sync="visible">
        <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()"
            label-width="120px">

            <ec-form-item-header value="用户基本信息"></ec-form-item-header>
            <el-row :gutter="15">
                <el-col :span="12">

                    <el-form-item label="用户名" prop="username">
                        <el-input clearable v-model="dataForm.username" placeholder="用户名" maxlength="10"
                            show-word-limit></el-input>
                    </el-form-item>
                </el-col>
                <el-col :span="12">
                    <el-form-item label="身份证号" prop="idCard">
                        <el-input clearable v-model="dataForm.idCard" placeholder="身份证号" maxlength="18" show-word-limit>
                        </el-input>
                    </el-form-item>
                </el-col>
            </el-row>
            <el-row :gutter="15">
                <el-col :span="12">

                    <el-form-item label="手机号" prop="phone">
                        <el-input clearable v-model="dataForm.phone" placeholder="手机号" maxlength="11" show-word-limit>
                        </el-input>
                    </el-form-item>
                </el-col>
                <el-col :span="12">
                    <el-form-item label="头像" prop="avatar">
                        <el-input clearable v-model="dataForm.avatar" placeholder="头像"></el-input>
                    </el-form-item>
                </el-col>
            </el-row>
            <el-row :gutter="15">
                <el-col :span="12">
                    <el-form-item label="用户类型" prop="userType">
                        <ec-dict-select v-model="dataForm.userType" placeholder="请选择用户类型" dictType="user_type">
                        </ec-dict-select>
                    </el-form-item>
                </el-col>
                <el-col :span="12">
                    <el-form-item label="邮箱" prop="email">
                        <el-input clearable v-model="dataForm.email" placeholder="邮箱" maxlength="20" show-word-limit>
                        </el-input>
                    </el-form-item>
                </el-col>
            </el-row>

            <ec-form-item-header value="用户业务信息"></ec-form-item-header>
            <el-row :gutter="15">
                <el-col :span="12">
                    <el-form-item label="所属集团" prop="pkGroup">
                        <ec-group-tree-select :visible="visible" v-model="dataForm.pkGroup" placeholder="请选择所属集团">
                        </ec-group-tree-select>
                    </el-form-item>
                </el-col>
                <el-col :span="12">
                    <el-form-item label="所属组织" prop="pkOrg">
                        <ec-org-tree-select :visible="visible" :pkGroup="dataForm.pkGroup" v-model="dataForm.pkOrg"
                            placeholder="请选择所属组织"></ec-org-tree-select>
                    </el-form-item>
                </el-col>
            </el-row>
            <el-row :gutter="15">
                <el-col :span="12">
                    <el-form-item label="所属部门" prop="pkDept">
                        <ec-dept-tree-select :visible="visible" :pkGroup="dataForm.pkGroup" :pkOrg="dataForm.pkOrg"
                            v-model="dataForm.pkDept" placeholder="请选择所属部门"></ec-dept-tree-select>
                    </el-form-item>
                </el-col>
                <el-col :span="12"></el-col>
            </el-row>

        </el-form>
        <span slot="footer" class="dialog-footer">
            <el-button @click="closeDailog">取消</el-button>
            <el-button type="primary" :loading="submitLoading" @click="dataFormSubmit()">确定</el-button>
        </span>
    </el-dialog>
</template>
<script>
    import * as userService from '@/api/upms/user'
    import * as dictService from '@/api/bdms/dict'
    import * as orgService from '@/api/upms/org'
    import * as groupService from '@/api/upms/group'
    export default {
        data() {
            return {
                visible: false,
                submitLoading: false,
                dataForm: {
                    userId: 0,
                    username: '',
                    accountName: '',
                    password: '',
                    phone: '',
                    idCard: '',
                    email: '',
                    userType: '',
                    avatar: '',
                    lockFlag: '',
                    delFlag: '',
                    revision: '',
                    pkGroup: '',
                    pkOrg: '',
                    pkDept: '',
                },
                dataRule: {
                    pkGroup: [{
                        required: false,
                        message: '所属集团不能为空',
                        trigger: 'change'
                    }],
                    pkOrg: [{
                        required: false,
                        message: '所属组织不能为空',
                        trigger: 'change'
                    }],
                    username: [{
                        required: true,
                        message: '用户名不能为空',
                        trigger: 'blur'
                    }],
                    password: [{
                        required: true,
                        message: '密码不能为空',
                        trigger: 'blur'
                    }],
                    salt: [{
                        required: true,
                        message: '随机盐不能为空',
                        trigger: 'blur'
                    }],
                    phone: [{
                        required: true,
                        message: '手机号不能为空',
                        trigger: 'blur'
                    }],
                    userType: [{
                        required: true,
                        message: '用户类型不能为空',
                        trigger: 'blur'
                    }],
                    lockFlag: [{
                        required: true,
                        message: '账号状态不能为空',
                        trigger: 'blur'
                    }],
                    delFlag: [{
                        required: true,
                        message: '删除状态不能为空',
                        trigger: 'blur'
                    }],
                }
            }
        },
        components: {},
        methods: {
            init(id) {
                this.dataForm.userId = id || 0
                this.visible = true
                this.$nextTick(() => {
                    this.$refs['dataForm'].resetFields()
                    if (this.dataForm.userId) {
                        userService.getuserinfo(this.dataForm.userId).then(data => {
                            this.dataForm.pkGroup = data.pkGroup
                            this.dataForm.pkOrg = data.pkOrg
                            this.dataForm.username = data.username
                            this.dataForm.password = data.password
                            this.dataForm.salt = data.salt
                            this.dataForm.phone = data.phone
                            this.dataForm.idCard = data.idCard
                            this.dataForm.email = data.email
                            this.dataForm.userType = data.userType
                            this.dataForm.avatar = data.avatar
                            this.dataForm.lockFlag = data.lockFlag
                            this.dataForm.delFlag = data.delFlag
                            this.dataForm.revision = data.revision

                            this.dataForm.pkGroup = data.pkGroup
                            this.dataForm.pkOrg = data.pkOrg
                            this.dataForm.pkDept = data.pkDept
                        })
                    }
                })
            },
            // 表单提交
            dataFormSubmit() {
                this.$refs['dataForm'].validate((valid) => {
                    if (valid) {
                        this.submitLoading = true
                        var params = {
                            'userId': this.dataForm.userId || undefined,
                            'pkGroup': this.dataForm.pkGroup,
                            'pkOrg': this.dataForm.pkOrg,
                            'username': this.dataForm.username,
                            'accountName': this.dataForm.accountName,
                            'password': this.dataForm.password,
                            'salt': this.dataForm.salt,
                            'phone': this.dataForm.phone,
                            'idCard': this.dataForm.idCard,
                            'email': this.dataForm.email,
                            'userType': this.dataForm.userType,
                            'avatar': this.dataForm.avatar,
                            'lockFlag': this.dataForm.lockFlag,
                            'delFlag': this.dataForm.delFlag,
                            'revision': this.dataForm.revision,
                            'pkDept': this.dataForm.pkDept,
                            'pkGroup': this.dataForm.pkGroup,
                            'pkOrg': this.dataForm.pkOrg,
                        }
                        var tick = !this.dataForm.userId ? userService.adduser(params) : userService
                            .updateuser(params)
                        tick.then(data => {
                            this.$message({
                                message: '操作成功',
                                type: 'success',
                                duration: 1500,
                                onClose: () => {
                                    this.submitLoading = false
                                    this.closeDailog()
                                    this.$emit('refreshDataList')
                                }
                            })
                        }).catch((res) => {
                            this.submitLoading = false
                            this.closeDailog()
                        })
                    }
                })
            },
            closeDailog() {
                this.$refs['dataForm'].resetFields()
                this.visible = false
            },
        }
    }
</script>